package com.yuanhua.sim.netty.utils;

import io.netty.channel.group.ChannelGroup;
import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author yuanhua
 */
public class SessionUtils {
	/**
	 * userID 映射 连接channel
	 */
	public static Map<Long, Channel> userIdChannelMap = new ConcurrentHashMap<>();
	
	/**
	 * groupId ---> channelgroup 群聊ID和群聊ChannelGroup映射
	 */
	private static Map<Integer, ChannelGroup> groupIdChannelGroupMap = new ConcurrentHashMap<>();

	public static Channel getChannel(Long userId) {
		return userIdChannelMap.get(userId);
	}


	public static Channel removeChannel(Long userId) {
		return userIdChannelMap.remove(userId);
	}
	/**
	 * 绑定群聊Id  群聊channelGroup
	 * @param groupId
	 * @param channelGroup
	 */
	public static void bindChannelGroup(Integer groupId, ChannelGroup channelGroup) {
		groupIdChannelGroupMap.put(groupId, channelGroup);
		System.out.println(channelGroup);
	}

	public static ChannelGroup getChannelGroup(Long groupId) {
		return groupIdChannelGroupMap.get(groupId);
	}

	public static Long getUserId(Channel channel) {
		for (Map.Entry<Long, Channel> longChannelEntry : userIdChannelMap.entrySet()) {
			if (longChannelEntry.getValue() == channel) {
				return longChannelEntry.getKey();
			}
		}
		return null;
	}
}